package com.woniuxy.patrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.common.utils.ResponseUtil;
import com.woniuxy.patrol.pojo.PatrolCategory;
import com.woniuxy.patrol.service.PatrolCategoryService;
import com.woniuxy.patrol.mapper.PatrolCategoryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 13162
* @description 针对表【wcm_patrol_category(巡更分类表)】的数据库操作Service实现
* @createDate 2025-07-14 19:22:37
*/
@Service
public class PatrolCategoryServiceImpl extends ServiceImpl<PatrolCategoryMapper, PatrolCategory>
    implements PatrolCategoryService{

    @Resource
    private PatrolCategoryMapper categoryMapper;

    @Override
    public ResponseUtil getByCommunityId(Long communityId) {
        // 对应《巡更demo.docx》中按社区筛选分类的功能
        List<PatrolCategory> categories = categoryMapper.selectByCommunityId(communityId);
        return ResponseUtil.success(categories);
    }

    @Override
    public ResponseUtil addCategory(PatrolCategory category) {
        // 校验分类名称唯一性
        QueryWrapper<PatrolCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_name", category.getCategoryName())
                .eq("community_id", category.getCommunityId())
                .eq("is_delete", 1);
        if (baseMapper.selectCount(queryWrapper) > 0) {
            return ResponseUtil.error("该分类名称已存在");
        }

        category.setStatus(1); // 默认为启用
        category.setIsDelete(1);
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        save(category);
        return ResponseUtil.success("巡更分类添加成功");
    }

    @Override
    public ResponseUtil updateCategory(PatrolCategory category) {
        category.setUpdateTime(new Date());
        updateById(category);
        return ResponseUtil.success("巡更分类更新成功");
    }

    @Override
    public ResponseUtil deleteCategory(Long categoryId) {
        PatrolCategory category = new PatrolCategory();
        category.setCategoryId(categoryId);
        category.setIsDelete(0); // 逻辑删除
        category.setUpdateTime(new Date());
        updateById(category);
        return ResponseUtil.success("巡更分类删除成功");
    }

}




